﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CaiyinSoft.NetGather;
using CaiyinSoft.NetGather.Protocol;
using CaiyinSoft.NetGather.NetGatherUtility;
using CaiyinSoft.NetGather.NetGatherLib.Filter;
using CaiyinSoft.NetGather.NetGatherLib.Analyzer;
using CaiyinSoft.NetGather.NetGatherLib.Publisher;
using CaiyinSoft.NetGather.NetGatherLib.URLSource;
using CaiyinSoft.NetGather.NetGatherLib.Filter.Process;
using CaiyinSoft.NetGather.NetGatherLib.Filter.Special;
using CaiyinSoft.NetGather.NetGatherLib.Filter.Generate;


namespace NetGather
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            richTextBox1.Text = CaiyinSoft.NetGather.NetGatherUtility.WebUtils.DoGet(textBox1.Text, System.Text.Encoding.GetEncoding("GBK"));

        }

        private void button2_Click(object sender, EventArgs e)
        {

            HTMLAnalyzerBase HTMLAnalyzer = new HTMLAnalyzerBase();
            
            TitleHTMLAnalyze title = new TitleHTMLAnalyze();
            KeywordsHTMLAnalyze keyword = new KeywordsHTMLAnalyze();
            DescriptionHTMLAnalyze desc = new DescriptionHTMLAnalyze();

            HTMLAnalyzer.AddFilter(title);
            HTMLAnalyzer.AddFilter(keyword);
            HTMLAnalyzer.AddFilter(desc);

            NextPageRegexHTMLAnalyze nextReg = new NextPageRegexHTMLAnalyze();
            nextReg.FilterExpression = "下一页";
            HTMLAnalyzer.AddFilter(nextReg);


            for (int i = 1; i <= 7;i++ )
            {
                XPathHTMLAnalyze content = new XPathHTMLAnalyze();
                content.OutputKey = string.Format("Content{0}", i);
                content.FilterExpression = string.Format(@"//div[@class='ArtCon']/p[@align='center'or @align='left'][{0}]", i);
                HTMLAnalyzer.AddFilter(content);

                if (i == 1 || i == 6)
                {
                    ImgSrcHTMLAnalyze contentImg = new ImgSrcHTMLAnalyze();
                    content.AddChildFilter (contentImg);

                    ImgSrcRegexHTMLAnalyze contentImg2 = new ImgSrcRegexHTMLAnalyze();
                    content.AddChildFilter(contentImg2);
                }
                else
                {
                    InnerTextHTMLAnalyze txtAnalyze = new InnerTextHTMLAnalyze();
                    content.AddChildFilter(txtAnalyze);
                }

            }


            IHTMLFilter content1 = HTMLAnalyzer.GetFilter("Content1");
            IHTMLFilter content6 = HTMLAnalyzer.GetFilter("Content6");
            IHTMLFilter content7 = HTMLAnalyzer.GetFilter("Content77");


            SimpleReplaceHTMLAnalyze titleReplace = new SimpleReplaceHTMLAnalyze();
            titleReplace.ReplaceFrom = "|yxlady.com";
            titleReplace.ReplaceTo = "";
            title.AddChildFilter(titleReplace);


            XPathHTMLAnalyze contentnext = new XPathHTMLAnalyze();
            contentnext.OutputKey = "nextPageLink";
            contentnext.FilterExpression = string.Format(@"//p[@class='ArtPage']/b/a[@class='nextpage'][1]");
            HTMLAnalyzer.NextHTMLFilter =contentnext;

            LinkHrefHTMLAnalyze nextLink = new LinkHrefHTMLAnalyze();
            contentnext.AddChildFilter(nextLink);


            bool bl = true;
            while (bl)
            {
                HTMLAnalyzer.SetInputHTML(  richTextBox1.Text);
                List<IHTMLFilter> convertResult =HTMLAnalyzer.Convert();
                foreach (var result in convertResult)
                {
                    richTextBox2.AppendText(string.Format("==={0}===", result.OutputKey));
                    richTextBox2.AppendText(Environment.NewLine);
                    List<FilterResult> frs = result.FilterHTML();
                    if (frs != null)
                    {
                        frs.Sort();
                        foreach (var fr in frs)
                        {
                            richTextBox2.AppendText(fr.HTMLResult);
                            richTextBox2.AppendText(Environment.NewLine);
                        }
                    }
                }

                string url=HTMLAnalyzer.NextPageUrl().Trim();
                if (url.Length > 0)
                {
                    textBox1.Text = url;
                    richTextBox1.Clear();
                    richTextBox1.Text = CaiyinSoft.NetGather.NetGatherUtility.WebUtils.DoGet(textBox1.Text, System.Text.Encoding.GetEncoding("GBK"));

                }
                else
                    bl = false;

            }




        }

        private void button3_Click(object sender, EventArgs e)
        {
            HTMLAnalyzerBase analyzer = new HTMLAnalyzerBase();

            GUIDHTMLAnalyze guid = new GUIDHTMLAnalyze();
            TitleHTMLAnalyze title = new TitleHTMLAnalyze();
            KeywordsHTMLAnalyze keyword = new KeywordsHTMLAnalyze();
            DescriptionHTMLAnalyze desc = new DescriptionHTMLAnalyze();

            guid.RunOnce = true;
            title.RunOnce = true;
            keyword.RunOnce = true;
            desc.RunOnce = true;

            guid.OutputKey = "GUID";
            title.OutputKey = "Title";
            keyword.OutputKey = "Title";
            desc.OutputKey = "Description";

            analyzer.AddFilter(guid);
            analyzer.AddFilter(title);
            analyzer.AddFilter(keyword);
            analyzer.AddFilter(desc);

            FormatHTMLAnalyze formatGuid = new FormatHTMLAnalyze();
            formatGuid.FilterExpression = "{{" + "{0}" + "}}";
            guid.AddChildFilter(formatGuid);

            for (int i = 1; i <= 7; i++)
            {
                XPathHTMLAnalyze content = new XPathHTMLAnalyze();
                content.OutputKey = "Content";// string.Format("Content{0}", i);
                content.FilterExpression = string.Format(@"//div[@class='ArtCon']/p[@align='center'or @align='left'][{0}]", i);
                analyzer.AddFilter(content);

                if (i == 1 || i == 6)
                {
                    ImgSrcHTMLAnalyze contentImg = new ImgSrcHTMLAnalyze();
                    content.AddChildFilter(contentImg);
                }
                else
                {
                    InnerTextHTMLAnalyze txtAnalyze = new InnerTextHTMLAnalyze();
                    content.AddChildFilter(txtAnalyze);
                }

            }


            IHTMLFilter content1 = analyzer.GetFilter("Content1");
            IHTMLFilter content6 = analyzer.GetFilter("Content6");
            IHTMLFilter content7 = analyzer.GetFilter("Content77");


            SimpleReplaceHTMLAnalyze titleReplace = new SimpleReplaceHTMLAnalyze();
            titleReplace.ReplaceFrom = "|yxlady.com";
            titleReplace.ReplaceTo = "";
            title.AddChildFilter(titleReplace);


            XPathHTMLAnalyze contentnext = new XPathHTMLAnalyze();
            contentnext.OutputKey = "nextPageLink";
            contentnext.FilterExpression = string.Format(@"//p[@class='ArtPage']/b/a[@class='nextpage'][1]");
            analyzer.NextHTMLFilter = contentnext;

            LinkHrefHTMLAnalyze nextLink = new LinkHrefHTMLAnalyze();
            contentnext.AddChildFilter(nextLink);

            SQLHTMLPublisher publisher = new SQLHTMLPublisher();
            publisher.ToExecuteSQL = "INSERT INTO [We7_CMS_DB].[dbo].[Article]([ID], [Title], [Description], [OwnerID], [ParentID], [AccountID], [Author], [Content], [State], [Created], [Updated], [Overdue], [SequenceIndex], [Source], [AllowComments], [Permission], [IsImage], [IsShow], [SubTitle], [Thumbnail], [IsDeleted], [ContentUrl], [ContentType], [SourceID], [SN], [ProcessState], [ProcessDirection], [ProcessSiteID], [FlowXml], [ChannelName], [FullChannelPath], [ChannelFullUrl], [CommentCount], [Tags], [ModelXml], [EnumState], [IndustryID], [FromRowID], [FromSiteUrl], [KeyWord], [DescriptionKey], [VideoCode], [ListKeys], [ListKeys1], [ListKeys2], [ListKeys3], [ListKeys4], [ListKeys5], [IPStrategy], [ModelName], [TableName], [ModelConfig], [ModelSchema], [PrivacyLevel], [Photos], [Clicks], [DayClicks], [YesterdayClicks], [WeekClicks], [MonthClicks], [QuarterClicks], [YearClicks], [Color], [FontWeight], [FontStyle]) " +
                " VALUES({@GUID},{@Title},{@Description},'{69f15b3b-d91d-4aa5-b45c-b2f5cd3e21de}','{00000000-0000-0000-0000-000000000000}','{00000000-0000-0000-0000-000000000000}','系统管理员',{@Content(text)},1,getdate(),getdate(),dateadd(year,2,getdate()),999,'NET',1,NULL,0,1,'','',0,'',0,'',113,99,'','','','/解决方案/政府机关','/解决方案/政府机关','/solution/gov/',0,'','','00000000000000000000',NULL,'','','','','','','','','','','','','','System.Article','','',0,null,0,0,0,0,0,0,0,'','','')";
            
            publisher.ConnectionStr = "Data Source=(local);Initial Catalog=We7_CMS_DB;Persist Security Info=True;User ID=sa;Password=123";
           
            CaiyinSoft.NetGather.NetGatherLib.URLConverter convert = new CaiyinSoft.NetGather.NetGatherLib.URLConverter();
            convert.Encoding = "GBK";
            convert.Interval = 10;
            convert.SetInputURL(  textBox1 .Text);
            convert.HTMLAnalyzer = analyzer;

            TextURLSource source = new TextURLSource();
            source.URLListText= (string.Format("{1}{0}{2}{0}{3}", Environment.NewLine, "aaa", "bbb", "ccc"));

            CaiyinSoft.NetGather.NetGatherLib.Task task = new CaiyinSoft.NetGather.NetGatherLib.Task();
            task.TaskName = "test";
            task.Converter = convert;
            task.URLSource = source;
            task.HTMLPubliser = publisher;


            CaiyinSoft.NetGather.NetGatherLib.Task.Save("c:\\aaa.tsk",task,false);
            

        }

        private void button4_Click(object sender, EventArgs e)
        {
            CaiyinSoft.NetGather.NetGatherLib.Task task = CaiyinSoft.NetGather.NetGatherLib.Task.Read("c:\\aaa.con");

            Dictionary<int, string> htmls = task.Converter.GetInputHTMLDic();
            task.HTMLPubliser.Publish(htmls, task.Converter.HTMLAnalyzer);

            Dictionary<string, List<FilterResult>>  analyzerResult = task.HTMLPubliser.GetAnalyzer(htmls, task.Converter.HTMLAnalyzer);

            foreach (var html in analyzerResult)
            {

                richTextBox2.AppendText(string.Format("==={0}===", html.Key));
                richTextBox2.AppendText(Environment.NewLine);

                List<FilterResult> frs = html.Value;
                if (frs != null)
                {
                    frs.Sort();
                    foreach (var fr in frs)
                    {
                        richTextBox2.AppendText(fr.HTMLResult);
                        richTextBox2.AppendText(Environment.NewLine);
                    }
                }


                //richTextBox2.AppendText(html.Value);
                richTextBox2.AppendText(Environment.NewLine);
            }

        }
    }
}
